package com.zhangmeng.windowportbind.utils;

import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.io.IOException;

public class AlertUtils {

    /**
     * 警告弹窗
     */
    public static Alert alert_warning(String msg) {
        Alert alert = new Alert(Alert.AlertType.WARNING);
        alert.initModality(Modality.APPLICATION_MODAL);
        alert.setContentText(msg);
        alert.setHeaderText(null);
        alert.show();
        return alert;
    }

    public static Alert  alert_msg(String msg) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.initModality(Modality.APPLICATION_MODAL);
        alert.setContentText(msg);
        alert.setHeaderText(null);
        alert.show();
        return alert;
    }

    public static void alert(String title, Parent node, Stage primaryStage) {
        alert(title, node, 600, 400, primaryStage,false);
    }

    public static void alert(String title, Parent node, int width, int height, Stage primaryStage, boolean isResizable) {
        Scene scene = new Scene(node);
        Stage stage = new Stage();
        stage.setScene(scene);
        stage.setTitle(title);
        stage.setWidth(width);
        stage.setHeight(height);
        stage.initStyle(StageStyle.UTILITY);
        stage.initOwner(primaryStage);
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.setResizable(isResizable);
        stage.show();
        stage.setOnCloseRequest(e -> {
            stage.close();
        });

    }

    public static void alert(String title, FXMLLoader loader, Stage primaryStage){
        try {
            Scene scene = new Scene(loader.load());
            Stage dialogStage = new Stage();
            dialogStage.setTitle(title);
            dialogStage.initModality(Modality.WINDOW_MODAL);
            dialogStage.initOwner(primaryStage);
            dialogStage.setScene(scene);
            dialogStage.showAndWait();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


}
